<?php 
	require_once("models/config.php");
	require_once("pie.php");

	require_once("graphs/graph-settings.php");
	
	if(!isUserLoggedIn()) {
		header ("location: login.php");
		die();
	}

	global $loggedInUser;
	global $db;

	$sql = "SELECT `root_Causes`.`SubcategoryId` AS `SubcategoryId` , `root_Cause_Subcategories`.`Name` AS `Name` , SUM( `Debugging_Time` ) AS DebuggingTime
		FROM `debugging_Reports`
		JOIN `root_Causes` ON `debugging_Reports`.`Root_Cause` = `root_Causes`.`Id` JOIN `root_Cause_Subcategories` ON `root_Causes`.`SubcategoryId` = `root_Cause_Subcategories`.`Id` JOIN `userCake_Users` ON `debugging_Reports`.`User` = `userCake_Users`.`User_ID`
		WHERE ((`Review_Reason` IS NULL) OR (`Reviewed` = 1)) AND (`userCake_Users`.`Group_ID` = " . STANDART_USER_GROUP_ID . ")
		GROUP BY `root_Causes`.`SubcategoryId`";
	
	$result = $db->sql_query($sql);
	if (!$result) {
		$error = $db->sql_error();
		return;
	}
		
	$data = array();
	$names = array();
	$rows = $db->sql_fetchall();
	foreach($rows as $i => $row) {
		$data[] = $row['DebuggingTime'];
		$names[] = $row['Name'];
	}

	drawPie($data, $names,
		GRAPH_WIDTH,
		GRAPH_HEIGHT,
		DEFAULT_PIE_RADIUS,
		DEFAULT_LABEL_LEFT,
		DEFAULT_PIE_TOP,
		DEFAULT_TITLE_BAR_HEIGHT,
		"Average debugging time for root causes");
?>
